package com.example.ymm.modules1.service.impl.ms;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.ms.MsMouldItem;
import com.example.ymm.modules1.entity.my.ms.MsMouldMain;
import com.example.ymm.modules1.mapper.my.ms.MsMouldItemMapper;
import com.example.ymm.modules1.service.dto.ms.MsMouldItemQueryCriteria;
import com.example.ymm.modules1.service.dto.ms.MsMouldOrderQueryCriteria;
import com.example.ymm.modules1.service.dto.ms.MsOrderDto;
import com.example.ymm.modules1.service.dto.ms.MsOrderListQueryCriteria;
import com.example.ymm.modules1.service.ms.MsMouldItemService;
import com.example.ymm.modules1.service.ms.TProviderPartnerService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//@Transactional
@Slf4j
@DS("slave2")
public class MsMouldItemServiceImpl extends BaseServiceImpl<MsMouldItemMapper, MsMouldItem> implements MsMouldItemService {

    @Autowired
    TProviderPartnerService tProviderPartnerService;

    @Override
    public TableList<MsMouldMain> list(MsMouldOrderQueryCriteria criteria, YmPageDto pageDto) {
//        IPage<MsMouldItem> page = new Page<MsMouldItem>(pageDto.getPage(),pageDto.getPageSize());
//        QueryWrapper predicate = QueryHelpPlus.getPredicate(MsMouldItem.class, criteria);
//        predicate.orderByDesc("create_time");
//        baseMapper.selectPage(page, predicate);
        getPage(pageDto);
        List<MsMouldMain> orderList = baseMapper.getOrderList(criteria);
        PageInfo<MsMouldMain> pageInfo = new PageInfo<>(orderList);
        TableList<MsMouldMain> tableList=new TableList(pageInfo);
        List<MsMouldMain> list = tableList.getList();
        if(UtilValidate.isNotEmpty(list)){
            list.forEach(r->{
                MsOrderListQueryCriteria orderQuery=new MsOrderListQueryCriteria();
                orderQuery.setOrderNoEq(r.getOrderNo());
                List<MsOrderDto> msOrderDtos = tProviderPartnerService.purchaseOrderList(orderQuery);
                if(UtilValidate.isNotEmpty(msOrderDtos)){
                    MsOrderDto msOrderDto = msOrderDtos.get(0);
                   // r.setOrderPrice(msOrderDto.getOrderPrice());
                    r.setSupplierName(msOrderDto.getSupplierName());
                }
            });
        }
        return tableList;
    }

    public List<MsMouldItem> queryAll(MsMouldItemQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(MsMouldItem.class, criteria);
        predicate.orderByDesc("create_time");
        List<MsMouldItem> result =baseMapper.selectList(predicate);
        return result;
    }


    @Override
    public List<MsMouldItem> getItems(String mouldMainId) {
        MsMouldItemQueryCriteria criteria=new MsMouldItemQueryCriteria();
        criteria.setMouldMainId(mouldMainId);

        return queryAll(criteria);
    }
}
